package com.dianping.cat.consumer.matrix.model.transform;
import com.dianping.cat.consumer.matrix.model.IVisitor;
import com.dianping.cat.consumer.matrix.model.entity.Matrix;
import com.dianping.cat.consumer.matrix.model.entity.MatrixReport;
import com.dianping.cat.consumer.matrix.model.entity.Ratio;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  15 */     this.m_linker = new DefaultLinker(true);




    this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

  public static MatrixReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static MatrixReport parse(InputStream in) {
/*  28 */     DefaultNativeParser parser = new DefaultNativeParser(in);
    MatrixReport matrixReport = new MatrixReport();

    try {
      matrixReport.accept(parser);
/*  33 */     } catch (RuntimeException e) {
/*  34 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  37 */         throw e;
      }
    }

/*  41 */     parser.m_linker.finish();
/*  42 */     return matrixReport;
  }



  public void visitMatrix(Matrix matrix) {
    byte tag;
/*  49 */     while ((tag = readTag()) != -1) {
/*  50 */       visitMatrixChildren(matrix, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitMatrixChildren(Matrix matrix, int _field, int _type) {
/*  55 */     switch (_field) {
      case 1:
/*  57 */         matrix.setType(readString());
        break;
      case 2:
/*  60 */         matrix.setName(readString());
        break;
      case 3:
/*  63 */         matrix.setCount(readInt());
        break;
      case 4:
/*  66 */         matrix.setTotalTime(readLong());
        break;
      case 5:
/*  69 */         matrix.setUrl(readString());
        break;
      case 33:
/*  72 */         if (_type == 1) {
/*  73 */           Ratio ratio = new Ratio();

/*  75 */           visitRatio(ratio);
/*  76 */           this.m_linker.onRatio(matrix, ratio); break;
/*  77 */         }  if (_type == 2) {
          for (int i = readInt(); i > 0; i--) {
/*  79 */             Ratio ratio = new Ratio();

/*  81 */             visitRatio(ratio);
/*  82 */             this.m_linker.onRatio(matrix, ratio);
          }
        }
        break;
    }
  }



  public void visitMatrixReport(MatrixReport matrixReport) {
    byte tag;
/*  93 */     if ((tag = readTag()) != -4) {
/*  94 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/*  97 */     while ((tag = readTag()) != -1) {
/*  98 */       visitMatrixReportChildren(matrixReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitMatrixReportChildren(MatrixReport matrixReport, int _field, int _type) {
/* 103 */     switch (_field) {
      case 1:
/* 105 */         matrixReport.setDomain(readString());
        break;
      case 2:
/* 108 */         matrixReport.setStartTime(readDate());
        break;
      case 3:
/* 111 */         matrixReport.setEndTime(readDate());
        break;
      case 4:
/* 114 */         if (_type == 1) {
/* 115 */           matrixReport.addDomain(readString()); break;
/* 116 */         }  if (_type == 2) {
/* 117 */           for (int i = readInt(); i > 0; i--) {
/* 118 */             matrixReport.addDomain(readString());
          }
        }
        break;
      case 33:
/* 123 */         if (_type == 1) {
/* 124 */           Matrix matrix = new Matrix();

/* 126 */           visitMatrix(matrix);
/* 127 */           this.m_linker.onMatrix(matrixReport, matrix); break;
/* 128 */         }  if (_type == 2) {
/* 129 */           for (int i = readInt(); i > 0; i--) {
/* 130 */             Matrix matrix = new Matrix();

/* 132 */             visitMatrix(matrix);
/* 133 */             this.m_linker.onMatrix(matrixReport, matrix);
          }
        }
        break;
    }
  }



  public void visitRatio(Ratio ratio) {
    byte tag;
/* 144 */     while ((tag = readTag()) != -1) {
/* 145 */       visitRatioChildren(ratio, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitRatioChildren(Ratio ratio, int _field, int _type) {
/* 150 */     switch (_field) {
      case 1:
/* 152 */         ratio.setType(readString());
        break;
      case 2:
/* 155 */         ratio.setMin(readInt());
        break;
      case 3:
/* 158 */         ratio.setMax(readInt());
        break;
      case 4:
/* 161 */         ratio.setTotalCount(readInt());
        break;
      case 5:
/* 164 */         ratio.setTotalTime(readLong());
        break;
      case 6:
/* 167 */         ratio.setUrl(readString());
        break;
    }
  }

  private Date readDate() {
    try {
/* 174 */       return new Date(readVarint(64));
/* 175 */     } catch (IOException e) {
/* 176 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 182 */       return (int)readVarint(32);
/* 183 */     } catch (IOException e) {
/* 184 */       throw new RuntimeException(e);
    }
  }

  private long readLong() {
    try {
/* 190 */       return readVarint(64);
/* 191 */     } catch (IOException e) {
/* 192 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 198 */       return this.m_in.readUTF();
/* 199 */     } catch (IOException e) {
/* 200 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 206 */       return this.m_in.readByte();
/* 207 */     } catch (IOException e) {
/* 208 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 213 */     int shift = 0;
/* 214 */     long result = 0L;

/* 216 */     while (shift < length) {
/* 217 */       byte b = this.m_in.readByte();
/* 218 */       result |= (b & 0x7F) << shift;
/* 219 */       if ((b & 0x80) == 0) {
/* 220 */         return result;
      }
/* 222 */       shift += 7;
    }

/* 225 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/matrix/model/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */